**********************************************************
*** This do.file prepares Panels B & C of Figure 2
**********************************************************

global db "D:\Dropbox\unequal_gains\main_data"
global resultspath "D:\Dropbox\unequal_gains\QJE revision plan\analysis\clean_results"
global Appendixresultspath "D:\Dropbox\unequal_gains\QJE revision plan\analysis\clean_results\appendix"

** Panel B & C **

* 1) Build a registry of income-group-specific & product-module-specific Feenstra ratios from 2004 to 2015
use "$db/Important Price Datasets/new_exit_income_final.dta", clear
drop ssnp ssep

egen id=group(income_q product_module_code)
tsset id year
gen ssepM_final=L.ssepM
drop if year==2004
order ssnpM ssepM_final
drop ssepM
rename ssepM_final ssepM
gen feenstraM=(1-ssnpM)/(1-ssepM)
replace feenstraM=1 if missing(feenstraM)
drop id

* 2) Bring in elasticities 
merge m:1 product_module_code using "$db/Important Price Datasets/sigmas"
drop _merge

gen feenstraM_common=feenstraM^(1/(sigma_common-1))
gen feenstraM_Q1Q5=0
replace feenstraM_Q1Q5=feenstraM^(1/(sigma_topQ-1)) if income_quintile==5
replace feenstraM_Q1Q5=feenstraM^(1/(sigma_bottomQ-1)) if income_quintile==1
gen feenstraM_Handbury=feenstraM^(1/(2.09-1))
gen feenstraM_Dube=feenstraM^(1/(4-1))
gen feenstraM_MR=feenstraM^(1/(7-1))
gen feenstraM_BW=feenstraM^(1/(11.5-1))

* winsorize at 1% level
foreach i in feenstraM_common feenstraM_Q1Q5 feenstraM_Handbury ///
feenstraM_Dube feenstraM_MR feenstraM_BW {
sum `i', d
replace `i'=r(p99) if `i'>r(p99)
replace `i'=r(p1) if `i'<r(p1)
}
save "$db/Important Price Datasets/FeenstraRatios", replace

* 3) Compute Nested-CES inflation with adjustment for new goods
* [uses the same code as for Figure 1a, but bringing Feenstra ratios]

clear
set obs 1 
generate nces_price_index_full_B=.
save "$db/Important Price Datasets/inflationFeenstra_income.dta", replace

foreach i of numlist 2004(1)2014 {

local var=`i'+1

use "$db/Important Price Datasets/price_index_household_income_`i'_2004_modules_final.dta", clear
rename total_quantity total_quantity_old
rename average_unit_price average_unit_price_old
rename average_unit_price_all average_unit_price_all_old

merge 1:1 upc upc_ver income_quintile using "$db/Important Price Datasets/price_index_household_income_`var'_2004_modules_final.dta"
keep if _merge==3
drop total_spending

gen double price_ratio=average_unit_price/average_unit_price_old
gen double price_ratio_all=average_unit_price_all/average_unit_price_all_old
* get rid of outliers
sum price_ratio, d
drop if price_ratio<r(p1) | price_ratio>r(p99)
sum price_ratio_all, d
drop if price_ratio_all<r(p1) | price_ratio_all>r(p99)

* NCES step 1: module level
drop total_spending*
bysort product_module_code income_quintile: egen double total_spendingM=sum(average_unit_price*total_quantity)
bysort product_module_code income_quintile: egen double total_spendingM_old=sum(average_unit_price_old*total_quantity_old)
gen double shareM=average_unit_price*total_quantity/total_spendingM
gen double shareM_old=average_unit_price_old*total_quantity_old/total_spendingM_old

bysort product_module_code income_quintile: gen double weightM_num=(shareM-shareM_old)/(ln(shareM)-ln(shareM_old))
bysort product_module_code income_quintile: egen double weightM_den=sum(weightM_num)
gen double weightM=weightM_num/weightM_den

gen double numeratorM_temp=weightM*log(price_ratio)
bysort product_module_code income_quintile: egen double numeratorM=sum(numeratorM_temp)
gen double nces_price_index=exp(numeratorM)

** NOW BRING IN FEENSTRA RATIOS
keep income_quintile nces_price_index ///
product_module_code product_group_code department_code ///
total_spendingM total_spendingM_old
duplicates drop 

gen year=`i'+1
merge 1:1 income_quintile product_module_code year using "$db/Important Price Datasets/FeenstraRatios"
drop if _merge==2
drop _merge

foreach z in feenstraM_common feenstraM_Q1Q5 feenstraM_Handbury ///
feenstraM_Dube feenstraM_MR feenstraM_BW {
replace `z'=1 if missing(`z')
}

gen nces_price_index_C=nces_price_index*feenstraM_common
gen nces_price_index_Q=nces_price_index*feenstraM_Q1Q5
gen nces_price_index_H=nces_price_index*feenstraM_Handbury
gen nces_price_index_D=nces_price_index*feenstraM_Dube
gen nces_price_index_MR=nces_price_index*feenstraM_MR
gen nces_price_index_BW=nces_price_index*feenstraM_BW
* rename the baseline
gen nces_price_index_B=nces_price_index

* NCES step 2: product group level

bysort product_group_code income_quintile: egen double total_spending_group=sum(total_spendingM)
bysort product_group_code income_quintile: egen double total_spending_group_old=sum(total_spendingM_old)
gen double share=total_spendingM/total_spending_group
gen double share_old=total_spendingM_old/total_spending_group_old

bysort product_group_code income_quintile: gen double weight_num=(share-share_old)/(ln(share)-ln(share_old))
bysort product_group_code income_quintile: egen double weight_den=sum(weight_num)
gen double weight=weight_num/weight_den

foreach z in B C Q H D MR BW {
gen double numerator_temp=weight*log(nces_price_index_`z')
bysort product_group_code income_quintile: egen double numerator=sum(numerator_temp)
gen double nces_price_index_group_`z'=exp(numerator)
drop numerator_temp numerator
}

* NCES step 3: department level
keep nces_price_index_group_* ///
product_group_code department_code ///
total_spending_group total_spending_group_old income_quintile
duplicates drop 

bysort department_code income_quintile: egen double total_spending_department=sum(total_spending_group)
bysort department_code income_quintile: egen double total_spending_department_old=sum(total_spending_group_old)
gen double share=total_spending_group/total_spending_department
gen double share_old=total_spending_group_old/total_spending_department_old

bysort department_code income_quintile: gen double weight_num=(share-share_old)/(ln(share)-ln(share_old))
bysort department_code income_quintile: egen double weight_den=sum(weight_num)
gen double weight=weight_num/weight_den

foreach z in B C Q H D MR BW {
gen double numerator_temp=weight*log(nces_price_index_group_`z')
bysort department_code income_quintile: egen double numerator=sum(numerator_temp)
gen double nces_price_index_dep_`z'=exp(numerator)
drop numerator_temp numerator
}

keep nces_price_index_dep_* ///
department_code total_spending_department total_spending_department_old income_quintile
duplicates drop 

* NCES step 4: overall level
bysort income_quintile: egen double total_spending_full=sum(total_spending_department)
bysort income_quintile: egen double total_spending_full_old=sum(total_spending_department_old)
gen double share=total_spending_department/total_spending_full
gen double share_old=total_spending_department_old/total_spending_full_old

gen double weight_num=(share-share_old)/(ln(share)-ln(share_old))
bysort income_quintile: egen double weight_den=sum(weight_num)
gen double weight=weight_num/weight_den

foreach z in B C Q H D MR BW {
gen double numerator_temp=weight*log(nces_price_index_dep_`z')
bysort income_quintile: egen double numerator=sum(numerator_temp)
gen double nces_price_index_full_`z'=exp(numerator)
drop numerator_temp numerator
}

keep nces_price_index_full_* income_quintile
duplicates drop 

gen year=`var'

append using "$db/Important Price Datasets/inflationFeenstra_income.dta"
drop if missing(nces_price_index_full_B)
save "$db/Important Price Datasets/inflationFeenstra_income.dta", replace 
}

* Panel B

use "$db/Important Price Datasets/inflationFeenstra_income.dta", clear

foreach i in B C Q H D MR BW {
gen `i'_infl=(nces_price_index_full_`i'-1)*100
}

collapse (mean) *_infl, by(income_quintile)

scatter C income_quintile || lfit C inc, lcolor(maroon) graphregion(color(white)) ///
 xtitle("Household Income Quintile") ytitle("Nested-CES Annual Inflation Rate," "Avg. 2004-2015, %") ///
 leg(off) ylabel(-0.6(0.2)0.8,angle(0))
graph export "$resultspath/Figure2PanelB.pdf", as(pdf) replace
graph save "$resultspath/Figure2PanelB.gph", replace

* Panel C

keep if income_quintile==1 | income_quintile==5
gsort - income_quintile
foreach i in B C Q H D MR BW {
gen `i'_infl_diff=`i'_infl-`i'_infl[_n-1]
tab `i'_infl_diff
}
keep *_infl_diff
keep C Q D BW
rename C index1
rename Q index2
rename D index3
rename BW index4
drop if missing(index1)
gen id=1
reshape long index, i(id) j(price_index)

scatter index price_index, ///
xlabel(1 `""Estimated Elast. " "for Each Module"' ///
 2 `""Estimated Elast. " "for Each Module" "by Income Quintile"' ///
 3 `""Elast. of 4," "Dube et al." "(2009)"' ///
 4 `""Elast. of 11.5," "Broda Weinstein" "(2010)"') ///
xtitle("") ytitle("Annual Inflation Difference b/w Bottom and" "Top Income Quintiles, Avg. 2004-2015, pp") ///
ylabel(0.60(0.10)1.20, angle(0)) ///
graphregion(color(white) margin(vlarge))
graph export "$resultspath/Figure2PanelC.pdf", as(pdf) replace
graph save "$resultspath/Figure2PanelC.gph", replace
